package com.pmpd.activity.service.api.impl;

import com.pmpd.activity.controller.api.param.ActivityWinningParam;
import com.pmpd.activity.dao.ActivityWinningMapper;
import com.pmpd.activity.po.ActivityWinning;
import com.pmpd.activity.po.ActivityWinningExample;
import com.pmpd.activity.service.api.ApiActivityWinningService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author liuying
 * @Description 活动获奖人信息服务层实现
 * @Date 15:09 2018/6/7
 */
@Service
public class ApiActivityWinningServiceImpl implements ApiActivityWinningService{

    @Resource
    private ActivityWinningMapper activityWinningMapper;

    @Override
    public ActivityWinning addWinnerMassage(ActivityWinningParam param) {
        ActivityWinning activityWinning = new ActivityWinning();
        activityWinning.setUserId(Long.parseLong(param.getUserId()));
        activityWinning.setUserName(param.getUserName());
        activityWinning.setPhone(param.getPhone());
        activityWinning.setAddr(param.getAddr());
        activityWinning.setCreateTime(System.currentTimeMillis()/1000);
        activityWinning.setEndTime(activityWinning.getCreateTime());
        activityWinning.setActivityId(Long.parseLong(param.getActivityId()));

        int success = activityWinningMapper.insert(activityWinning);
        return (success == 1) ? activityWinning : null;
    }

    @Override
    public int isAward(long activityId, long userId) {
        ActivityWinningExample activityWinningExample = new ActivityWinningExample();
        activityWinningExample.createCriteria().andActivityIdEqualTo(activityId).andUserIdEqualTo(userId);

        return activityWinningMapper.countByExample(activityWinningExample);
    }

}
